from flask import Flask, request, render_template,send_from_directory
from werkzeug.utils import secure_filename
import os

UPLOAD_PATH = os.path.join(os.path.dirname(__file__), 'imgs')

app = Flask(__name__)


@app.route('/upload/', methods=['GET', 'POST'])
def upload():
    if request.method == 'GET':
        return render_template('upload.html')
    else:
        img_file = request.files.get('pic')
        file_name = img_file.filename
        # 文件名的安全转换
        filename = secure_filename(file_name)
        # 保存文件
        img_file.save(os.path.join(UPLOAD_PATH, filename))
        return '上传成功'


@app.route('/download/<filename>')
def download(filename):
    return send_from_directory(UPLOAD_PATH,filename)


if __name__ == '__main__':
    app.run(debug=True)
